package com.lanou3g.dllo.baidumusic.musiclibrary.recommend.allsinger;


import android.content.Context;
import android.media.Image;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.MyVolley;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;
import com.lanou3g.dllo.baidumusic.urlvalues.UrlValues;

import java.util.List;

/**
 * Created by dllo on 16/6/27.
 */
public class AllSingerAdapter extends PagerAdapter {

    private AllSingerBean allSingerBean;
    private List<AllSingerBean.ArtistBean> list;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public AllSingerAdapter(Context context) {
        this.context = context;
    }

    public void setAllSingerBean(AllSingerBean allSingerBean) {
        this.allSingerBean = allSingerBean;
        list = allSingerBean.getArtist();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // 设置每页3张照片
        return list == null ? 0 : list.size() / 3;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LinearLayout item = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_viewpager, container, false);
        ImageView allSingerPictureOne = (ImageView) item.findViewById(R.id.allsinger_picture_one);
        TextView allSingerSingerOne = (TextView) item.findViewById(R.id.allsinger_singer_one);
        ImageView allSingerPictureTwo = (ImageView) item.findViewById(R.id.allsinger_picture_two);
        TextView allSingerSingerTwo = (TextView) item.findViewById(R.id.allsinger_singer_two);
        ImageView allSingerPictureThree = (ImageView) item.findViewById(R.id.allsinger_picture_three);
        TextView allSingerSingerThree = (TextView) item.findViewById(R.id.allsinger_singer_three);
        final int pos = position * 3;
        allSingerSingerOne.setText(list.get(pos).getName());
        allSingerSingerTwo.setText(list.get(pos + 1).getName());
        allSingerSingerThree.setText(list.get(pos + 2).getName());

        SingleVolley.getSingleVolley(context).getImageLoader().get(allSingerBean.getArtist().get(pos).getAvatar_big(), ImageLoader.getImageListener(allSingerPictureOne, R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        SingleVolley.getSingleVolley(context).getImageLoader().get(allSingerBean.getArtist().get(pos + 1).getAvatar_big(), ImageLoader.getImageListener(allSingerPictureTwo, R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        SingleVolley.getSingleVolley(context).getImageLoader().get(allSingerBean.getArtist().get(pos + 2).getAvatar_big(), ImageLoader.getImageListener(allSingerPictureThree, R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        // 给图片设置监听
        allSingerPictureOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(pos);
            }
        });

        allSingerPictureTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(pos+1);
            }
        });

        allSingerPictureThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(pos+2);
            }
        });

        container.addView(item);
        return item;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

    interface OnItemClickListener {
        void onItemClick(int pos);
    }

}
